package router

import (
	"n-server/internal/handler"
	"n-server/internal/middleware"

	"github.com/gin-gonic/gin"
)

func Setup(
	userHandler *handler.UserHandler,
	healthHandler *handler.HealthHandler,
) *gin.Engine {
	r := gin.New()

	// 全局中间件
	r.Use(middleware.GinLogger())       // 请求日志
	r.Use(middleware.GinRecovery(true)) // 崩溃恢复，带堆栈信息
	r.Use(middleware.Cors())            // 跨域处理
	r.Use(middleware.RateLimit())       // 限流处理

	// Swagger 路由
	// r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// API 路由组
	api := r.Group("/api")
	{
		// 健康检查路由
		api.GET("/health", healthHandler.HealthCheck)

		// 用户相关
		userGroup := api.Group("/user")
		{
			userGroup.POST("/register", userHandler.RegisterUser)
			userGroup.POST("/login", userHandler.LoginUser)
			userGroup.POST("/delete", middleware.AuthMiddleware(), middleware.Authorize("admin"), userHandler.DeleteUser)
			userGroup.POST("/update", middleware.AuthMiddleware(), userHandler.UpdateUser)
			userGroup.POST("/list", middleware.AuthMiddleware(), middleware.Authorize("admin"), userHandler.ListUsers)
			userGroup.GET("/admin/demo", middleware.AuthMiddleware(), middleware.Authorize("admin"), userHandler.AdminDemo)
		}

		// 其他业务路由...
	}

	return r
}
